<?php
  $i_nghi_nv = 0;
  $i_phat_nv = 0;
  $i_phat_to = 0;
?>
<form method="post" action="<?php echo base_path() . 'to_san_xuat/tien_do_san_xuat/nhap_ket_qua/' . $data['id_tien_do'] ?>" id="ket-qua-san-xuat-form">
  <fieldset>
    <legend>Kết quả sản xuất</legend>
    <div id="form-item-ket-qua-wrapper">
      <div class="form-item form-text-wrapper to-san-xuat-wrapper">
        <label>Tổ sản xuất:</label>
        <label class="data-text tsx-data-text"><?php echo $data['tien_do']->ten_to_san_xuat . ' [' . $data['tien_do']->ma_to_san_xuat . ']' ?></label>
      </div>
      <div class="form-item form-text-wrapper ngay-thang-wrapper">
        <label>Ngày tháng:</label>
        <label class="data-text ngay-thang-data-text"><?php echo date('n/j/Y', strtotime($data['tien_do']->ngay_thang)) ?></label>
      </div>
      <div class="clear"></div>
      <div class="form-item form-text-wrapper so-cong-nhan-wrapper">
        <label>Số công nhân:</label>
        <label class="data-text scn-data-text"><?php echo $data['tien_do']->so_cong_nhan ?></label>
      </div>
      <div class="form-item form-text-wrapper so-cong-nhat-wrapper">
        <label for="txt-so-cong-nhat">Số công:</label>
        <input id="txt-so-cong-nhat" value="<?php echo ($_POST) ? $_POST['so_cong_nhat'] : '' ?>" name="so_cong_nhat" type="text" autocomplete="OFF"
                       class="form-text text txt-so-cong-nhat <?php echo ($_POST && (string) $_POST['error']['key'] == 'so_cong_nhat') ? 'error' : '' ?>" />
      </div>
      <div class="clear"></div>
      <div class="form-item form-text-wrapper chi-tieu-wrapper">
        <label>Chỉ tiêu:</label>
        <label id="ct-data-text" class="data-text ct-data-text"><?php echo $data['tien_do']->chi_tieu ?></label>
      </div>
      <div class="form-item form-text-wrapper ket-qua-wrapper">
        <label for="scl-ket-qua">Kết quả:</label>
        <select id="scl-ket-qua" name="ket_qua"class="form-select select scl-ket-qua">
          <option value="1" <?php echo ($_POST && $_POST['ket_qua'] == 1 ? 'selected="selected"' : '') ?>>Không đạt</option>
          <option value="2" <?php echo (!$_POST || ($_POST && $_POST['ket_qua'] == 2) ? 'selected="selected"' : '') ?>>Hoàn thành</option>
          <option value="3" <?php echo ($_POST && $_POST['ket_qua'] == 3 ? 'selected="selected"' : '') ?>>Vượt chỉ tiêu</option>
        </select>
      </div>
      <div class="clear"></div>
      <div class="form-item form-text-wrapper so-hong-wrapper">
        <label for="txt-so-hong">Hàng lỗi:</label>
        <input id="txt-so-hong" value="<?php echo ($_POST) ? $_POST['so_hong'] : '0' ?>" name="so_hong" type="text" autocomplete="OFF"
                       class="form-text text txt-so-hong <?php echo ($_POST && (string) $_POST['error']['key'] == 'so_hong') ? 'error' : '' ?>" />
      </div>
      <div class="form-item form-text-wrapper thuc-te-wrapper">
        <label for="txt-thuc-te">Thực tế:</label>
        <input id="txt-thuc-te" value="<?php echo ($_POST) ? $_POST['thuc_te'] : '' ?>" name="thuc_te" type="text" autocomplete="OFF"
                       class="form-text text txt-thuc-te <?php echo ($_POST && (string) $_POST['error']['key'] == 'thuc_te') ? 'error' : '' ?>" />
      </div>
      <div class="clear"></div>
      <div class="form-item form-textarea-wrapper">
        <label for="ghi-chu">Nguyên nhân - Khắc phục</label>
        <textarea id="ghi-chu" name="ghi_chu" cols="60" rows="3"
          class="form-textarea <?php echo ($_POST && (string) $_POST['error']['key'] == 'ghi_chu') ? 'error' : '' ?>"><?php echo ($_POST) ? $_POST['ghi_chu'] : '' ?></textarea>
        <div class="description">Nhập tối đa 500 ký tự. Số lượng hiện thời: <span style="color: red;" id="ghi-chu-length">0</span></div>
      </div>
      <div class="clear"></div>
    </div>
  </fieldset>
  <fieldset style="display: none;">
    <legend>Nhân viên nghỉ làm</legend>
    <div id="form-item-nghi-nv-wrapper">
      <?php if ($_POST && isset($_POST['nghi_nv'])) { ?>
      <?php foreach ($_POST['nghi_nv'] as $key => $nghi_nv) { ?>
        <?php $i_nghi_nv++ ?>
        <div class="form-item-row">
          <div class="form-item form-text-wrapper">
            <label for="txt-nghi-nv-<?php echo $i_nghi_nv ?>">Nhân viên</label>
            <input id="txt-nghi-nv-<?php echo $i_nghi_nv ?>" value="<?php echo $nghi_nv ?>" name="nghi_nv[]" type="text" autocomplete="OFF"
                   class="form-text form-autocomplete text txt-nghi-nv <?php echo (isset($_POST['error']['nghi_nv']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>" />
            <?php if ($i_nghi_nv > 1) { ?>
            <input class="autocomplete" type="hidden" id="txt-nghi-nv-<?php echo $i_nghi_nv ?>-autocomplete"
                   value="<?php echo base_path() . 'nhan_vien/'.$data['tien_do']->id_tsx.'/autocomplete' ?>" disabled="disabled"/>
            <?php } ?>
          </div>
          <div class="form-item">
            <input id="field-remove-nghi-nv-submit-<?php echo $i_nghi_nv ?>" type="button" value="X"
                   class="field-remove-submit form-submit"/>
          </div>
          <div class="form-item form-select-wrapper">
            <select id="scl-thoi-luong-nghi-<?php echo $i_nghi_nv ?>" name="thoi_luong_nghi[]"class="form-select select scl-thoi-luong-nghi">
              <option value="1" <?php echo ($_POST['thoi_luong_nghi'][$key] == 1 ? 'selected="selected"' : '') ?>>Cả ngày</option>
              <option value="0.5" <?php echo ($_POST['thoi_luong_nghi'][$key] == 0.5 ? 'selected="selected"' : '') ?>>Nửa ngày</option>
            </select>
          </div>
          <div class="form-item form-select-wrapper">
            <select id="scl-ly-do-nghi-<?php echo $i_nghi_nv ?>" name="ly_do_nghi[]"class="form-select select scl-ly-do-nghi">
              <option value="Nghỉ phép năm" <?php echo ($_POST['ly_do_nghi'][$key] == 'Nghỉ phép năm' ? 'selected="selected"' : '') ?>>Nghỉ phép năm</option>
              <option value="Nghỉ ốm" <?php echo ($_POST['ly_do_nghi'][$key] == 'Nghỉ ốm' ? 'selected="selected"' : '') ?>>Nghỉ ốm</option>
              <option value="Nghỉ thai sản" <?php echo ($_POST['ly_do_nghi'][$key] == 'Nghỉ thai sản' ? 'selected="selected"' : '') ?>>Nghỉ thai sản</option>
              <option value="Nghỉ không phép" <?php echo ($_POST['ly_do_nghi'][$key] == 'Nghỉ phép năm' ? 'selected="selected"' : '') ?>>Nghỉ không phép</option>
            </select>
          </div>
          <div class="clear"></div>
        </div>
        <?php } ?>
      <?php } else { ?>
      <?php $i_nghi_nv++; ?>
      <div class="form-item-row">
        <div class="form-item form-text-wrapper">
          <label for="txt-nghi-nv-<?php echo $i_nghi_nv ?>">Nhân viên</label>
          <input id="txt-nghi-nv-<?php echo $i_nghi_nv ?>" value="" name="nghi_nv[]" type="text" autocomplete="OFF"
                 class="form-text form-autocomplete text txt-nghi-nv" />
        </div>
        <div class="form-item">
          <input id="field-remove-nghi-nv-submit-<?php echo $i_nghi_nv ?>" type="button" value="X"
                 class="field-remove-submit form-submit"/>
        </div>
        <div class="form-item form-select-wrapper">
          <label for="scl-thoi-luong-nghi-<?php echo $i_nghi_nv ?>">Thời gian</label>
          <select id="scl-thoi-luong-nghi-<?php echo $i_nghi_nv ?>" name="thoi_luong_nghi[]"class="form-select select scl-thoi-luong-nghi">
            <option value="1" selected="selected">Cả ngày</option>
            <option value="0.5">Nửa ngày</option>
          </select>
        </div>
        <div class="form-item form-select-wrapper">
          <label for="scl-ly-do-nghi-<?php echo $i_nghi_nv ?>">Lý do</label>
          <select id="scl-ly-do-nghi-<?php echo $i_nghi_nv ?>" name="ly_do_nghi[]"class="form-select select scl-ly-do-nghi">
            <option value="Nghỉ phép năm" selected="selected">Nghỉ phép năm</option>
            <option value="Nghỉ ốm">Nghỉ ốm</option>
            <option value="Nghỉ thai sản">Nghỉ thai sản</option>
            <option value="Nghỉ không phép">Nghỉ không phép</option>
          </select>
        </div>
        <div class="clear"></div>
      </div>
      <?php } ?>
    </div>
    <div id="add-more-nghi-nv-wrapper">
      <input id="add-more-nghi-nv" type="button" value="Thêm nghỉ" class="field-add-more-nghi-nv-submit form-submit"/>
    </div>
  </fieldset>
  <fieldset>
    <legend>Phạt lỗi nhân viên</legend>
    <div id="form-item-phat-nv-wrapper">
      <?php if ($_POST && isset($_POST['phat_nv'])) { ?>
      <?php foreach ($_POST['phat_nv'] as $key => $phat_nv) { ?>
        <?php $i_phat_nv++ ?>
        <div class="form-item-row">
          <div class="form-item form-text-wrapper">
            <label for="txt-phat-nv-<?php echo $i_phat_nv ?>">Nhân viên</label>
            <input id="txt-phat-nv-<?php echo $i_phat_nv ?>" value="<?php echo $phat_nv ?>" name="phat_nv[]" type="text" autocomplete="OFF"
                   class="form-text form-autocomplete text txt-phat-nv <?php echo (isset($_POST['error']['phat_nv']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>" />
            <?php if ($i_phat_nv > 1) { ?>
            <input class="autocomplete" type="hidden" id="txt-phat-nv-<?php echo $i_phat_nv ?>-autocomplete"
                   value="<?php echo base_path() . 'nhan_vien/'.$data['tien_do']->id_tsx.'/autocomplete' ?>" disabled="disabled"/>
            <?php } ?>
          </div>
          <div class="form-item">
            <input id="field-remove-phat-nv-submit-<?php echo $i_phat_nv ?>" type="button" value="X"
                   class="field-remove-submit form-submit"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-tien-phat-nv-<?php echo $i_phat_nv ?>">Tiền phạt</label>
            <input id="txt-tien-phat-nv-<?php echo $i_phat_nv ?>" value="<?php echo $_POST['tien_phat_nv'][$key] ?>" name="tien_phat_nv[]" type="text" autocomplete="OFF"
                   class="format_currency form-text text txt-tien-phat-nv <?php echo (isset($_POST['error']['tien_phat_nv']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>" />
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-ly-do-phat-nv-<?php echo $i_phat_nv ?>">Lý do</label>
            <input id="txt-ly-do-phat-nv-<?php echo $i_phat_nv ?>" value="<?php echo $_POST['ly_do_phat_nv'][$key] ?>" name="ly_do_phat_nv[]" type="text" autocomplete="OFF"
                   class="form-text text txt-ly-do-phat-nv <?php echo (isset($_POST['error']['ly_do_phat_nv']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>" />
          </div>
          <div class="clear"></div>
        </div>
        <?php } ?>
      <?php } else { ?>
      <?php $i_phat_nv++; ?>
      <div class="form-item-row">
        <div class="form-item form-text-wrapper">
          <label for="txt-phat-nv-<?php echo $i_phat_nv ?>">Nhân viên</label>
          <input id="txt-phat-nv-<?php echo $i_phat_nv ?>" value="" name="phat_nv[]" type="text" autocomplete="OFF"
                 class="form-text form-autocomplete text txt-phat-nv" />
        </div>
        <div class="form-item">
          <input id="field-remove-phat-nv-submit-<?php echo $i_phat_nv ?>" type="button" value="X"
                 class="field-remove-submit form-submit"/>
        </div>
        <div class="form-item form-select-wrapper">
          <label for="txt-tien-phat-nv-<?php echo $i_phat_nv ?>">Tiền phạt</label>
            <input id="txt-tien-phat-nv-<?php echo $i_phat_nv ?>" value="" name="tien_phat_nv[]" type="text" autocomplete="OFF"
                   class="format_currency form-text text txt-tien-phat-nv" />
        </div>
        <div class="form-item form-select-wrapper">
          <label for="txt-ly-do-phat-nv-<?php echo $i_phat_nv ?>">Lý do</label>
            <input id="txt-ly-do-phat-nv-<?php echo $i_phat_nv ?>" value="" name="ly_do_phat_nv[]" type="text" autocomplete="OFF"
                   class="form-text text txt-ly-do-phat-nv" />
        </div>
        <div class="clear"></div>
      </div>
      <?php } ?>
    </div>
    <div id="add-more-phat-nv-wrapper">
      <input id="add-more-phat-nv" type="button" value="Thêm phạt" class="field-add-more-phat-nv-submit form-submit"/>
    </div>
  </fieldset>
  <fieldset>
    <legend>Phạt lỗi tổ sản xuất</legend>
    <div id="form-item-phat-to-wrapper">
      <?php if ($_POST && isset($_POST['tien_phat_to'])) { ?>
      <?php foreach ($_POST['tien_phat_to'] as $key => $tien_phat_to) { ?>
        <?php $i_phat_to++ ?>
        <div class="form-item-row">
          <div class="form-item form-text-wrapper">
            <label for="txt-tien-phat-to-<?php echo $i_phat_to ?>">Tiền phạt</label>
            <input id="txt-tien-phat-to-<?php echo $i_phat_to ?>" value="<?php echo $tien_phat_to ?>" name="tien_phat_to[]" type="text" autocomplete="OFF"
                   class="format_currency form-text text txt-tien-phat-to <?php echo (isset($_POST['error']['tien_phat_to']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>" />
          </div>
          <div class="form-item">
            <input id="field-remove-phat-to-submit-<?php echo $i_phat_to ?>" type="button" value="X"
                   class="field-remove-submit form-submit"/>
          </div>
          <div class="form-item form-text-wrapper">
            <label for="txt-ly-do-phat-to-<?php echo $i_phat_to ?>">Lý do</label>
            <input id="txt-ly-do-phat-to-<?php echo $i_phat_to ?>" value="<?php echo $_POST['ly_do_phat_to'][$key] ?>" name="ly_do_phat_to[]" type="text" autocomplete="OFF"
                   class="form-text text txt-ly-do-phat-to <?php echo (isset($_POST['error']['ly_do_phat_to']) && $_POST['error']['key'] == $key) ? 'error' : '' ?>" />
          </div>
          <div class="clear"></div>
        </div>
        <?php } ?>
      <?php } else { ?>
      <?php $i_phat_to++; ?>
      <div class="form-item-row">
        <div class="form-item form-select-wrapper">
          <label for="txt-tien-phat-to-<?php echo $i_phat_to ?>">Tiền phạt</label>
            <input id="txt-tien-phat-to-<?php echo $i_phat_to ?>" value="" name="tien_phat_to[]" type="text" autocomplete="OFF"
                   class="format_currency form-text text txt-tien-phat-to" />
        </div>
        <div class="form-item">
          <input id="field-remove-phat-to-submit-<?php echo $i_phat_to ?>" type="button" value="X"
                 class="field-remove-submit form-submit"/>
        </div>
        <div class="form-item form-select-wrapper">
          <label for="txt-ly-do-phat-to-<?php echo $i_phat_to ?>">Lý do</label>
            <input id="txt-ly-do-phat-to-<?php echo $i_phat_to ?>" value="" name="ly_do_phat_to[]" type="text" autocomplete="OFF"
                   class="form-text text txt-ly-do-phat-to" />
        </div>
        <div class="clear"></div>
      </div>
      <?php } ?>
    </div>
    <div id="add-more-phat-to-wrapper">
      <input id="add-more-phat-to" type="button" value="Thêm phạt" class="field-add-more-phat-to-submit form-submit"/>
    </div>
  </fieldset>
  <div id="submit-wrapper">
    <input class="autocomplete" type="hidden" id="txt-nghi-nv-1-autocomplete"
           value="<?php echo base_path() . 'nhan_vien/'.$data['tien_do']->id_tsx.'/autocomplete' ?>" disabled="disabled"/>
    <input class="autocomplete" type="hidden" id="txt-phat-nv-1-autocomplete"
           value="<?php echo base_path() . 'nhan_vien/'.$data['tien_do']->id_tsx.'/autocomplete' ?>" disabled="disabled"/>
    <input type="hidden" id="total-nghi-nv" value="<?php echo $i_nghi_nv  ?>" disabled="disabled"/>
    <input type="hidden" id="total-phat-nv" value="<?php echo $i_phat_nv  ?>" disabled="disabled"/>
    <input type="hidden" id="total-phat-to" value="<?php echo $i_phat_to  ?>" disabled="disabled"/>
    <input id="submit" type="submit" onclick="return check_nhap_ket_qua();" value="Nhập kết quả" class="form-submit"/>
<!--    <input id="submit" type="submit" value="Nhập dữ liệu" class="form-submit" style="display: none;"/>-->
  </div>
</form>